

public class MyBlockQueue {
    //创建一个顺序表队列
    private String[] elems;
    //提供一个构造方法,初始化elems数组
    public MyBlockQueue(int capcity){
        this.elems=new String[capcity];
    }
    private Object locker=new Object();
    private int head=0;
    private int tail=0;
    private int size=0;


    public void put(String s) throws InterruptedException {
        synchronized (locker){
            if (size==elems.length){
                //如果当前队列元素满了,我们就要停下来等待,其他线程调用take方法取走元素
                locker.wait();
            }
            //将s存放到elems数组tail的位置
            elems[tail]=s;
            //tail++
            tail++;
            if (tail>=elems.length){
                tail=0;
            }
            size++;
            locker.notify();
        }
    }


    public String take() throws InterruptedException {
        String elem=null;

        synchronized (locker){
            while (size==0){
                locker.wait();
            }
            elem=elems[head];
            head++;
            if (head==elems.length){
                head=0;
            }
            size--;

            locker.notify();
            return elem;
        }
    }
}
